package com.wosiliujing.learning.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.wosiliujing.learning.constant.CommonConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author: liujing
 * @date: 2019/5/3 21:10
 * @description: 响应信息主体
 */
@ToString
@Accessors(chain = true)
@JsonInclude(value=JsonInclude.Include.NON_NULL)
public class R<T>  implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code = CommonConstants.SUCCESS;

    @Getter
    @Setter
    private String msg = "success";


    @Getter
    @Setter
    private T data;

    public R() {
        super();
    }

    public R(T data) {
        super();
        this.data = data;
    }

    public R(int code,String msg) {
        super();
        this.msg = msg;
        this.code = code;
    }

    public R(int code,String msg,T data) {
        super();
        this.msg = msg;
        this.data = data;
        this.code = code;
    }

    public R(T data, String msg) {
        super();
        this.data = data;
        this.msg = msg;
    }
    public R(String msg,T data) {
        super();
        this.msg = msg;
        this.data = data;
    }

    public static R ok(){
        return new R();
    }

    public static R data(Object data){
        return new R(data);
    }

    public static  R error(){
        return new R(CommonConstants.FAIL.intValue(),"系统异常，请联系管理员");
    }

    public static  R error(String msg){
        return new R(CommonConstants.FAIL.intValue(),msg);
    }

    public static  R error(int code,String msg){
        return new R(code,msg);
    }
    public R(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = CommonConstants.FAIL;
    }

}
